// pages/index/index.js
Page({
  data: {
    welcomeText: '欢迎使用 Todo & Habits',
    features: [
      {
        icon: '/assets/icons/todo.png',
        title: '待办事项',
        description: '管理您的日常任务，提高工作效率'
      },
      {
        icon: '/assets/icons/habits.png',
        title: '习惯养成',
        description: '培养良好习惯，记录每日进度'
      }
    ]
  },

  onLoad: function () {
    // 页面加载时执行
    console.log('首页加载完成')
    // 初始化一些模拟数据，方便预览效果
    this.initMockData()
  },

  // 初始化模拟数据
  initMockData: function() {
    // 检查是否已有待办数据
    const todoList = wx.getStorageSync('todoList') || []
    const completedList = wx.getStorageSync('completedList') || []
    const habitsList = wx.getStorageSync('habitsList') || []
    
    // 如果没有数据，添加一些模拟数据
    if (todoList.length === 0) {
      const today = new Date().toLocaleString()
      const mockTodos = [
        {
          id: Date.now() - 1000,
          content: '完成微信小程序开发',
          createTime: today
        },
        {
          id: Date.now() - 2000,
          content: '学习新的前端技术',
          createTime: today
        },
        {
          id: Date.now() - 3000,
          content: '整理项目文档',
          createTime: today
        }
      ]
      wx.setStorageSync('todoList', mockTodos)
    }
    
    if (completedList.length === 0) {
      const today = new Date().toLocaleString()
      const mockCompleted = [
        {
          id: Date.now() - 4000,
          content: '创建项目基本结构',
          createTime: today,
          completeTime: today
        }
      ]
      wx.setStorageSync('completedList', mockCompleted)
    }
    
    if (habitsList.length === 0) {
      const today = new Date().toLocaleDateString()
      const yesterday = new Date(Date.now() - 86400000).toLocaleDateString()
      const mockHabits = [
        {
          id: Date.now() - 5000,
          name: '每日运动30分钟',
          createDate: yesterday,
          streak: 2,
          longestStreak: 2,
          completedDates: [yesterday, today],
          lastCompletedDate: today
        },
        {
          id: Date.now() - 6000,
          name: '阅读一小时',
          createDate: yesterday,
          streak: 1,
          longestStreak: 1,
          completedDates: [yesterday],
          lastCompletedDate: yesterday
        }
      ]
      wx.setStorageSync('habitsList', mockHabits)
    }
  },

  // 跳转到待办页面
  goToTodo: function() {
    wx.switchTab({
      url: '/pages/todo/todo'
    })
  },

  // 跳转到习惯页面
  goToHabits: function() {
    wx.switchTab({
      url: '/pages/habits/habits'
    })
  }
})